package com.jsnkj.basic

import android.app.Application
import android.content.Context
import com.hzhj.openads.HJAdsSdk
import com.jsnkj.basic.advertisement.AdvertisementManager
import com.jsnkj.basic.config.SpConfig
import com.jsnkj.basic.util.AESUtils
import com.jsnkj.basic.util.LogUtils
import com.yang.http.HttpRequest
import com.yizooo.tallybook.db.DBManager
import kotlin.properties.Delegates

open class AdLibApplication : Application() {


    companion object {
        var context: Context by Delegates.notNull()
            private set

        @JvmStatic
        fun getInstance(): AdLibApplication =context as AdLibApplication

        const val adAppId: String = "48530" // 静态常量，编译时常量
    }

    override fun onCreate() {
        super.onCreate()
       context = applicationContext
        HttpRequest.mDefaultBaseUrl= SpConfig.HTTP_URL;
        //         注册全局异常处理类
      CrashExceptionHandler.newInstance().init(applicationContext)

        SpConfig.DOWN_APK_PATH = getExternalCacheDir()!!.getPath() + "/down_apk/"

        try {
            AESUtils.key = AESUtils.encrypt1(
                "rU1Hp9fPwB2ru76DrgtP1SBkzs9Y9/+z2kfYsj0jD0k=",
               AESUtils.key,
                "7fdd593c623e1625f076c498c21e818c"
            )
        } catch (e: Exception) {
           AESUtils.key = "qebf78XWucK/sGpNS9RM9w=="
        }

        try{
            val hjAdsSdk = HJAdsSdk.sharedAds()
            hjAdsSdk.setAdult(true)
            hjAdsSdk.setDebugEnable(true)
            hjAdsSdk.setPersonalizedAdvertisingOn(true)
            hjAdsSdk.startWithAppId(this, adAppId);
            AdvertisementManager.isInit = true
        }catch (e:Exception){
           LogUtils.e("初始化灰鲸广告失败",e)
        }
        DBManager.initDB(applicationContext)
    }
}